<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/js/layui/css/layui.css" media="all">
</head>
<body>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="sendMaster">派单</button>
    </div>
</script>


<script src="/static/js/layui/layui.js" charset="utf-8"></script>
<script>
    var order = window.parent.order;
    layui.use(['table', 'jquery'], function () {
        var table = layui.table
            , $ = layui.jquery;
        table.render({
            elem: '#test'
            , height: 'full'
            , url: '/master/queryList'
            , method: 'post'
            , toolbar: '#toolbarDemo'
            , title: '用户数据表'
            , cellMinWidth: 80
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: '工号'}
                , {field: 'name', title: '姓名', edit: 'text'}
                , {field: 'address', title: '位置', templet: res => res.masterAddress.address}
                , {field: 'status', title: '状态', templet: res => status(res.masterAddress.status)}
            ]]
            , page: true
            , parseData: function (res) { //res 即为原始返回的数据
                if (res.code == 200) {
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.total, //解析数据长度
                        "data": res.list //解析数据列表
                    };
                }
            }
            , response: {
                statusCode: 200
            }
        });
        //工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'sendMaster':
                    var data = checkStatus.data;
                    if (data.length != 1) {
                        layer.msg("一次只能指派一个订单！");
                        return false;
                    } else {
                        $.post("/orders/dispatch", {"mid": data[0].id, "id": order.id}, function (res) {
                            if (res.code == 200) {
                                layer.msg("派单成功！");
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index);
                                window.parent.location.reload();
                            }
                        });
                    }
                    break;
            }
            ;
        });
    });

    function status(d) {
        var str = "";
        if (d == '0') {
            str = "在忙";
        }
        if (d == '1') {
            str = "空闲";
        }
        if (d == '2') {
            str = "其他";
        }
        return str;
    }
</script>
</body>
</html>